package chenji.yourticket.model;

import java.util.HashMap;

import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryAction;

import chenji.yourticket.Main;
import chenji.yourticket.inventory.InventoryManager.OpeningInventoryType;

public class ModelManager {
	public final HashMap<Player, Model> playerModelMap = new HashMap<Player, Model>();
	public final HashMap<Player, ActionChooseInventory> Actmap = new HashMap<Player, ActionChooseInventory>();

	public void openInventory(Player p) {
		Model m = new Model(p, Main.getPlayerDataChenter().getPlayerData(p).getTreeSet());
		m.openInventory(OpeningInventoryType.ModelManager);
		this.playerModelMap.put(p, m);
	}

	public void closeInventory(Player p) {
		this.playerModelMap.remove(p);
		this.Actmap.remove(p);
	}

	public void reloadInventory(Player p) {
		this.closeInventory(p);
		this.openInventory(p);
	}

	public void clickInventory(Player p, int slot, InventoryAction a) {
		if (this.playerModelMap.containsKey(p)) {
			this.playerModelMap.get(p).ClickItem(slot, a);
			return;
		}
		if (this.Actmap.containsKey(p)) {
			this.Actmap.get(p).clickInventory(slot);
			return;
		}
	}

	public void openActionChooseInventory(Player p, ModelInfo m) {
		ActionChooseInventory ac = new ActionChooseInventory(p, m);
		ac.openInventory(OpeningInventoryType.ModelManager);
		this.Actmap.put(p, ac);
	}

	public void unload() {
		for (Player p : this.Actmap.keySet()) {
			p.closeInventory();
		}
		for (Player p : this.playerModelMap.keySet()) {
			p.closeInventory();
		}
	}

}
